package com.facotry.controller;

import com.facotry.entity.ProduceOrder;
import com.facotry.service.ProduceOrderService;
import com.facotry.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * ------ 天道酬勤 -------
 *
 * @author ChenZihao
 * @date 2024/7/23
 * @desc --- 代码敲烂 月薪过万 ---
 */

@RestController
@RequestMapping("/produce")
public class ProduceOrderController {

    @Autowired
    private ProduceOrderService produceOrderService;


    @GetMapping("/getList")
    public R findAll(@RequestParam HashMap<String, String> map) {
        System.out.println("map = " + map);
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);// 开始分页
        List<ProduceOrder> list = produceOrderService.findAll(map);
        PageInfo<ProduceOrder> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }


    @GetMapping("/findProduct")
    public R findProduct(String productCode) {
        long l = Integer.parseInt(productCode);
        Object o = produceOrderService.findProduct(l);
        return R.ok(o);
    }

    @GetMapping("/addProduceOrder")
    public R addProduceOrder(ProduceOrder produceOrder) {
        System.out.println("produceOrder = " + produceOrder);
        boolean isOk = produceOrderService.add(produceOrder);
        return R.ok(isOk);
    }


    @GetMapping("/delBatch")
    public R delBatch(@RequestParam List<String> ids) {
        int i = produceOrderService.delBatch(ids);
        if (i > 0) {
            return R.ok();
        }
        return R.fail();

    }

    @GetMapping("/delOrderByNum")
    public R delOrderByNum(String num) {
        boolean isOk = produceOrderService.delOrderByNum(num);

        return R.ok(isOk);
    }


    @PostMapping("/editOrder")
    public R editOrder(@RequestBody ProduceOrder produceOrder){
       boolean isOk = produceOrderService.editOrder(produceOrder);

        return  R.ok(isOk);
    }

}
